<template>
  <div class="qr-input" :class="{'qr-input--suffix': showSuffix}">
    <input ref="input"
      class="qr-input__inner"
      :class="{'is-disabled': disabled}"
      :placeholder="placeholder"
      :type="showPassword ? (passwordVisible ? 'text':'password'): type"
      :disabled="disabled"
      :value="value"
      @input="handleInput"
    >
    <span class="qr-input__suffix" v-if="showSuffix">
      <i v-if="clearable && this.value"
        class="qr-input__icon qr-icon-circle-close qr-input__clear"
        @click="clear"
      ></i>
    </span>
  </div>
</template>

<script>
export default {
  name: 'QrInput',
  data () {
    return {
      passwordVisible: false
    }
  },
  props: {
    placeholder: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: 'text'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    value: [String, Number],
    clearable: {
      type: Boolean,
      default: false
    },
    showPassword: {
      type: Boolean,
      default: false
    },
    iconUser: {
     type: Boolean,
     default: false
    }
  },
  computed: {
    showSuffix () {
      return this.clearable || this.showPassword 
    }
  },
  methods: {
    handleInput (e) {
      this.$emit('input', e.target.value)
    },
    clear () {
      // console.log('123')
      this.$emit('input', '')
      this.$refs.input.focus()
    },
    handlePasswordVisible () {
      this.passwordVisible = !this.passwordVisible
      this.$refs.input.focus()
      // this.$refs.input.value = this.value
    }
  }
}
</script>

<style lang="scss">
.qr-input {
  width: 180px;
  position: relative;
  font-size: 14px;
  display: inline-block;
  .qr-input__inner {
    cursor: pointer;
    -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    color: #000;
    display: inline-block;
    font-size: inherit;
    height: 40px;
    line-height: 40px;
    outline: none;
    padding: 0 15px;
    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    width: 100%;

    &:focus {
      outline: none;
      border-color: #409eff;
    }
    &.is-disabled {
      background-color: #f5f7fa;
      border-color: #e4e7ed;
      color: #c0c4cc;
      cursor: not-allowed;
    }
  }

}

.qr-input--suffix {
  .qr-input__inner {
    padding-right: 30px;
  }
  .qr-input__suffix {
    position: absolute;
    height: 100%;
    right: 10px;
    top: 0;
    line-height: 40px;
    text-align: center;
    color: #c0c4cc;
    transition: all .3s;
    z-index: 900;
    i {
      color: #c0c4cc;
      font-size: 16px;
      cursor: pointer;
      transition: color .2s cubic-bezier(.645,.045,.355,1);
    }
  }
} 

</style>
